package com.kinyx.framework.security;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.business.KRandomUtils;
import com.kinyx.framework.enums.impl.RandomRange;
import com.kinyx.framework.file.KPathUtils;

public class KRSA_Tester_ {

	private final static String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIYFBReB2zGhMUlnDCIQsQzrUP3qfcGupjh1erpr2lOwrene9p4xFWGX8H0XgJ2FrWeRZ5OVb7qar9oj5rtJP+RofWWXGLvzEEpiDS2Zpo3R3TyVAwOoXYz37obrBBKsAAwbJ9QUjBHIjBo6MMNlkRgnUpLGbBsjM6vReDZE+KQbAgMBAAECgYB64G9itjT6tAg0fL6rHRK3/aksparXwgG1EkGMWwa6ZOuP2lXThL1N1bkxeDL8Omxa4P5OWl0uglVpdDFA1lA1DxEZGtBba+n3bb8o1tZ/2P6JAFokADaY9sZsua0HhLbWasWMACg/m7fL3c2ShercM6pLwAOGEmz+2vQS92uKgQJBAMXru1ikIvXCmoiMyb93oOw+CztMrKHSA1i4VM9PGL2oSN9uW145kbFOmFWlo+5TaKw1VtKJ/IJQ1QEviPE/ZNkCQQCtWOGlld7XlZapynWM0SI0mY9+TeySwLyWdHN6ysAeRWDcpi+CzeIQQXZ8LJMHDLuWN4PTGSNoq2oFEQcfLGgTAkBuKDHxuAktN87CyXlQc8jBNBcUPiynNDhgnNe3OpLOnGdS1B1OaERKpy/qG3MOJqZPkrBsJhjAfzTOoCJcA6ihAkEAiyA14kcZ1h8qD9X9aGqFMIA6yZ1TDSmBcqO990R40xLNvennYe1JzdAsf1brHhJbECI4PBoF8ED7U1uqQpvKXwJASc5j6EUDekoNrtjNgARDpFqm5pksqpIE/WZS7kFJH176EBaiXLWhs1jryvgNd1R6Ys0QZfx9VQa+gJkxYNMFjw==";
	private final static String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCGBQUXgdsxoTFJZwwiELEM61D96n3BrqY4dXq6a9pTsK3p3vaeMRVhl/B9F4Cdha1nkWeTlW+6mq/aI+a7ST/kaH1llxi78xBKYg0tmaaN0d08lQMDqF2M9+6G6wQSrAAMGyfUFIwRyIwaOjDDZZEYJ1KSxmwbIzOr0Xg2RPikGwIDAQAB";

	@Test
	public void testLength1() {
		for (int i = 0; i < 1000; i++) {
			final String plaintext = KRandomUtils.randomString(false, KRandomUtils.random(1, 117), RandomRange.NUMBER_AND_LETTER_FULL);
			Assert.assertEquals(plaintext, KRSA.decrypt(KRSA.encrypt(plaintext, PUBLIC_KEY, null), PRIVATE_KEY, null));
		}
	}

	@Test
	public void testLength2() {
		for (int i = 0; i < 1000; i++) {
			final String plaintext = KRandomUtils.randomString(false, KRandomUtils.random(117, 1000), RandomRange.NUMBER_AND_LETTER_FULL);
			Assert.assertEquals(plaintext, KRSA.decrypt(KRSA.encrypt(plaintext, PUBLIC_KEY, null), PRIVATE_KEY, null));
		}
	}

	private String filePath = KPathUtils.resources(true) + "/file/rsa";

	@Test
	public void test1() {
		Assert.assertEquals("-----BEGIN PRIVATE KEY-----PRIVATEKEY-----END PRIVATE KEY-----", KRSA.readPrimaryKey(this.filePath + "/rsa.privateKey", null, true, false));
		Assert.assertEquals("PRIVATEKEY", KRSA.readPrimaryKey(this.filePath + "/rsa.privateKey", null, false, false));

		Assert.assertEquals("-----BEGIN PUBLIC KEY-----PUBLICKEY-----END PUBLIC KEY-----", KRSA.readPublicKey(this.filePath + "/rsa.publicKey", null, true, false));
		Assert.assertEquals("PUBLICKEY", KRSA.readPublicKey(this.filePath + "/rsa.publicKey", null, false, false));
	}

	@Test
	public void test2() {
		Assert.assertEquals("-----BEGIN PRIVATE KEY-----" + "\n" + "PRIVATE" + "\n" + "KEY" + "\n" + "-----END PRIVATE KEY-----", KRSA.readPrimaryKey(this.filePath + "/rsa.privateKey", null, true, true));
		Assert.assertEquals("PRIVATE" + "\n" + "KEY", KRSA.readPrimaryKey(this.filePath + "/rsa.privateKey", null, false, true));

		Assert.assertEquals("-----BEGIN PUBLIC KEY-----" + "\n" + "PUBLIC" + "\n" + "KEY" + "\n" + "-----END PUBLIC KEY-----", KRSA.readPublicKey(this.filePath + "/rsa.publicKey", null, true, true));
		Assert.assertEquals("PUBLIC" + "\n" + "KEY", KRSA.readPublicKey(this.filePath + "/rsa.publicKey", null, false, true));
	}

	@Test
	public void test3() {
		Assert.assertEquals("PRIVATEKEY", KRSA.readPrimaryKey(this.filePath + "/rsa2.privateKey", null, true, false));
		Assert.assertEquals("PRIVATEKEY", KRSA.readPrimaryKey(this.filePath + "/rsa2.privateKey", null, false, false));

		Assert.assertEquals("PUBLICKEY", KRSA.readPublicKey(this.filePath + "/rsa2.publicKey", null, true, false));
		Assert.assertEquals("PUBLICKEY", KRSA.readPublicKey(this.filePath + "/rsa2.publicKey", null, false, false));
	}

	@Test
	public void test4() {
		Assert.assertEquals("PRIVATE" + "\n" + "KEY", KRSA.readPrimaryKey(this.filePath + "/rsa2.privateKey", null, true, true));
		Assert.assertEquals("PRIVATE" + "\n" + "KEY", KRSA.readPrimaryKey(this.filePath + "/rsa2.privateKey", null, false, true));

		Assert.assertEquals("PUBLIC" + "\n" + "KEY", KRSA.readPublicKey(this.filePath + "/rsa2.publicKey", null, true, true));
		Assert.assertEquals("PUBLIC" + "\n" + "KEY", KRSA.readPublicKey(this.filePath + "/rsa2.publicKey", null, false, true));
	}

}
